#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/12/21 11:30
# @Author : 闫峰
# @File    : get_sku_sales.py
import requests


def get_platform_sales(sku_list: list[str], orderSourceType, sku_sales_days, platformIdList=None):
    """
    得到各个平台销量
    """
    sku_list = list(set(sku_list))
    url = """https://api-dw.zhcxkj.com/dw/erp/salesvolume/platform/salessku"""
    if orderSourceType != "all":
        param = {
            "input": {
                "customerId": 1,
                "orderSourceType": orderSourceType,
                "skus": sku_list,
                "days": sku_sales_days
            }
        }
    else:
        param = {
            "input": {
                "customerId": 1,
                "orderSourceType": None,
                "skus": sku_list,
                "days": sku_sales_days
            }
        }
    hearders = {
        "Content-Type": "application/json",
        "Authorization": "Basic aXJvYm90Ym94OmkxcjVvOEJvdGJveA==",
    }

    res = requests.post(url=url, headers=hearders, json=param)
    sales = {}
    for sku in sku_list:
        sales[sku] = 0
    if isinstance(orderSourceType, int):
        for data in res.json().get('data'):
            if data.get('orderSourceType') == orderSourceType:
                if data["sku"] not in sales:
                    sales[data["sku"]] = data["salesVolume"]
                else:
                    sales[data["sku"]] += data["salesVolume"]
        return sales
    else:
        for data in res.json().get('data'):
            if platformIdList:
                if data['orderSourceType'] in platformIdList:
                    sales[data["sku"]] += data["salesVolume"]
            else:
                sales[data["sku"]] += data["salesVolume"]
        return sales


if __name__ == '__main__':
    sku_list = ["16551525"]
    orderSourceType = "all"
    sku_sales_days = 90
    platformIdList = [1,2]
    res = get_platform_sales(sku_list,orderSourceType,sku_sales_days,platformIdList)
    print(res)